package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.mapper.OmsOrderReturnReasonMapper;
import com.macro.mall.model.OmsOrderReturnReason;
import com.macro.mall.model.OmsOrderReturnReasonExample;
import com.macro.mall.model.UmsAdmin;
import com.macro.mall.service.OmsOrderReturnReasonService;
import com.macro.mall.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单原因管理Service实现类
 */
@Service
public class OmsOrderReturnReasonServiceImpl implements OmsOrderReturnReasonService {
    @Autowired
    private OmsOrderReturnReasonMapper returnReasonMapper;
    @Autowired
    private UmsAdminService adminService;
    @Override
    public int create(String currentUsername, OmsOrderReturnReason returnReason) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUsername);
        returnReason.setMemberId(admin.getId());
        returnReason.setStoreId(admin.getStoreId());
        returnReason.setMemberUserName(admin.getUserName());
        returnReason.setCreateTime(new Date());
        return returnReasonMapper.insert(returnReason);
    }

    @Override
    public int update(String currentUsername, Long id, OmsOrderReturnReason returnReason) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUsername);
        if(!admin.getStoreId().equals(returnReason.getStoreId())){
            return -2;
        }
        returnReason.setId(id);
        return returnReasonMapper.updateByPrimaryKeySelective(returnReason);
    }

    @Override
    public int delete(String currentUsername, List<Long> ids) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUsername);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids).andStoreIdEqualTo(admin.getStoreId());
        return returnReasonMapper.deleteByExample(example);
    }

    @Override
    public List<OmsOrderReturnReason> list(String currentUsername, Integer pageSize, Integer pageNum) {
        UmsAdminDTO admin = adminService.getAdminDTOByUserName(currentUsername);
        PageHelper.startPage(pageNum,pageSize);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andStoreIdEqualTo(admin.getStoreId());
        example.setOrderByClause("sort desc");
        return returnReasonMapper.selectByExample(example);
    }

    @Override
    public int updateStatus(List<Long> ids, Integer status) {
        if(!status.equals(0)&&!status.equals(1)){
            return 0;
        }
        OmsOrderReturnReason record = new OmsOrderReturnReason();
        record.setStatus(status);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);
        return returnReasonMapper.updateByExampleSelective(record,example);
    }

    @Override
    public OmsOrderReturnReason getItem(Long id) {
        return returnReasonMapper.selectByPrimaryKey(id);
    }
}
